﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace WinFormsApp1
{
    public partial class Registration : UIEditForm
    {
        public Registration()
        {
            InitializeComponent();
        }

        private void uiLabel1_Click(object sender, EventArgs e)
        {

        }

        private SqlCommand Cmd(String sql, SqlConnection conn)
        {
            SqlCommand cmd = new SqlCommand(sql, conn);
            return cmd;
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("XMLFile1.xml");
            string sqlServer = xmlDoc.SelectSingleNode("/DatabaseConfig/SQLServer").InnerText;
            string sqlCatalog = xmlDoc.SelectSingleNode("/DatabaseConfig/SQLCatalog").InnerText;
            string sdeUser = xmlDoc.SelectSingleNode("/DatabaseConfig/SDEUser").InnerText;
            string sdePwd = xmlDoc.SelectSingleNode("/DatabaseConfig/SDEPassword").InnerText;
            string str = "Data Source=" + sqlServer + "; Initial Catalog=sde;User ID=" + sdeUser + ";Password=" + sdePwd;//用户密码登录
            SqlConnection conn = new SqlConnection(str);
            if (conn.State == System.Data.ConnectionState.Closed)
            {
                conn.Open();
            }
            try
            {
                String Selectsql = "Select * From orduser where userID = '" + uiTextBox1.Text + "'";
                using (SqlCommand cmd = new SqlCommand(Selectsql, conn))
                {
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            ShowErrorTip("用户名已存在");
                            IsOK = false;
                        }
                    }
                }
                if (IsOK)
                {
                    string Insertsql = "INSERT INTO orduser (UserID, Password)  VALUES ('" + uiTextBox1.Text + "', '" + uiTextBox2.Text + "')";
                    using (SqlCommand cmd = new SqlCommand(Insertsql, conn))
                    {
                        cmd.ExecuteNonQuery();
                        ShowSuccessTip("注册成功");
                    }
                }
            }
            catch (Exception err)
            {
                ShowErrorTip(err.Message);
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }

            }



            
            

        }
    }
}
